import unittest
from string_extractor import StringExtractor

class TestStringExtractor(unittest.TestCase):

    def setUp(self):
        """每个测试前初始化一个 StringExtractor 实例"""
        self.extractor = StringExtractor()
        print("\n[INFO] Initialized a new StringExtractor instance")

    def test_basic_string(self):
        """测试基本字符串提取"""
        source = '老师说，"Hello, World!"'
        print(f"[TEST] Testing with input: {source}")
        for ch in source:
            self.extractor.trigger(ch)

        result = self.extractor.get_result()
        print(f"[RESULT] Extracted string: {result}")
        self.assertEqual(result, 'Hello, World!')

    def test_escape_characters(self):
        """测试带有转义字符的字符串"""
        source = '老师说，"你好，世界！\\\"转义字符测试\\\""'
        print(f"[TEST] Testing with input: {source}")
        for ch in source:
            self.extractor.trigger(ch)

        result = self.extractor.get_result()
        print(f"[RESULT] Extracted string: {result}")
        self.assertEqual(result, '你好，世界！\"转义字符测试\"')

    def test_empty_string(self):
        """测试空字符串"""
        source = ''
        print(f"[TEST] Testing with input: (empty string)")
        for ch in source:
            self.extractor.trigger(ch)

        result = self.extractor.get_result()
        print(f"[RESULT] Extracted string: {result}")
        self.assertEqual(result, '')

    def test_multiple_strings(self):
        """测试多个字符串的提取"""
        source = '老师说，"你好"，然后说，"世界！"'
        print(f"[TEST] Testing with input: {source}")
        for ch in source:
            self.extractor.trigger(ch)

        result = self.extractor.get_result()
        print(f"[RESULT] Extracted string: {result}")
        self.assertEqual(result, '你好世界！')

    def test_mixed_escape_and_quotes(self):
        """测试复杂的字符串，包含转义字符和双引号"""
        source = '程序员说，"这是一个\\\"复杂的\\\"例子！"'
        print(f"[TEST] Testing with input: {source}")
        for ch in source:
            self.extractor.trigger(ch)

        result = self.extractor.get_result()
        print(f"[RESULT] Extracted string: {result}")
        self.assertEqual(result, '这是一个\"复杂的\"例子！')

    def test_no_strings(self):
        """测试没有字符串的输入"""
        source = '没有双引号的文本'
        print(f"[TEST] Testing with input: {source}")
        for ch in source:
            self.extractor.trigger(ch)

        result = self.extractor.get_result()
        print(f"[RESULT] Extracted string: {result}")
        self.assertEqual(result, '')

    def test_only_quotes(self):
        """测试只有双引号的情况"""
        source = '" "'
        print(f"[TEST] Testing with input: {source}")
        for ch in source:
            self.extractor.trigger(ch)

        result = self.extractor.get_result()
        print(f"[RESULT] Extracted string: {result}")
        self.assertEqual(result, '')

    def test_complex_input(self):
        """测试复杂输入，包含多个转义字符和字符串"""
        source = 'Hello "world", this is a test "with \\"escaped\\" quotes".'
        print(f"[TEST] Testing with input: {source}")
        for ch in source:
            self.extractor.trigger(ch)

        result = self.extractor.get_result()
        print(f"[RESULT] Extracted string: {result}")
        self.assertEqual(result, 'worldwith \"escaped\" quotes')

if __name__ == '__main__':
    unittest.main()

